﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GraphSharp;
using GraphSharp.Controls;
using QuickGraph;
using WPFExtensions;

namespace snat {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {

        private IBidirectionalGraph<object, IEdge<object>> _snatGraph;
        public IBidirectionalGraph<object, IEdge<object>> SnatGraph {
            get { return _snatGraph; }
        }

        public MainWindow() {

            CreateGraph();
            InitializeComponent();

        }

        private bool CreateGraph() {
            var g = new BidirectionalGraph<object, IEdge<object>>();

            string[] vertices = new string[100];
            for (int i = 0; i < 100; i++) {
                vertices[i] = i.ToString();
                g.AddVertex(vertices[i]);
            }

            for (int i = 1; i < 5; i++) {
                g.AddEdge(new Edge<object>(vertices[i * 0], vertices[i * 1]));
                g.AddEdge(new Edge<object>(vertices[i * 1], vertices[i * 2]));
                g.AddEdge(new Edge<object>(vertices[i * 2], vertices[i * 3]));
                g.AddEdge(new Edge<object>(vertices[i * 3], vertices[i * 1]));
                g.AddEdge(new Edge<object>(vertices[i * 1], vertices[i * 4]));
                g.AddEdge(new Edge<object>(vertices[i * 5], vertices[i * 1]));
                g.AddEdge(new Edge<object>(vertices[i * 7], vertices[i * 2]));
                g.AddEdge(new Edge<object>(vertices[i * 9], vertices[i * 6]));
                g.AddEdge(new Edge<object>(vertices[i * 1], vertices[i * 8]));
                g.AddEdge(new Edge<object>(vertices[i * 4], vertices[i * 5]));
                g.AddEdge(new Edge<object>(vertices[i * 9], vertices[i * 5]));
            }


            _snatGraph = g;

            return true;
        }

        private bool CreateGraph(object[] vertices, Edge<string>[] edges) {
            var g = new BidirectionalGraph<object, IEdge<object>>();

            for (int i = 0; i < vertices.LongCount(); i++) {
                if (!g.AddVertex(vertices[i].ToString())) return false;
            }

            for (int i = 0; i < edges.LongCount(); i++) {
                if (!g.AddEdge(new Edge<object>(edges[i].Source.ToString(), edges[i].Target.ToString()))) return false;
            }

            _snatGraph = g;
            return true;
        }

        // Make sure you don't call this with a string that's already present in the graph
        public bool CreateNode(string s) {

            // Must be a better way of doing this?
            var g = new BidirectionalGraph<object, IEdge<object>>();
            g = _snatGraph.CopyToBidirectionalGraph<object, IEdge<object>>();
            if (g.AddVertex(s)) {
                _snatGraph = g;
                return true;
            }
            return false;
        }

        public bool CreateEdge(string fromNode, string toNode) {

            if (_snatGraph.ContainsVertex(fromNode) && _snatGraph.ContainsVertex(toNode) 
                            && !_snatGraph.ContainsEdge(new Edge<object>(fromNode, toNode))) {

                // Must be a better way of doing this?
                var g = new BidirectionalGraph<object, IEdge<object>>();
                g = _snatGraph.CopyToBidirectionalGraph<object, IEdge<object>>();
                if (g.AddEdge(new Edge<object>(fromNode, toNode))) {
                    _snatGraph = g;
                    return true;
                }
            }
            return false;
        }

        public void SetZoomMaxMin(float max, float min) {
            WPFExtensions.Controls.ZoomControl zoom = new WPFExtensions.Controls.ZoomControl();
            Console.WriteLine("Max = {0}, Min = {1}", zoom.MaxZoom, zoom.MinZoom);
            zoom.MaxZoom = max;
            zoom.MinZoom = min;
            Console.WriteLine("Max = {0}, Min = {1}", zoom.MaxZoom, zoom.MinZoom);
        }
    }
}

